package com.mygdx.game.group;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.mygdx.game.MainGame;
import com.mygdx.game.group.basegroup.BaseGroup;
import com.mygdx.game.resource.Res;

public class CardGroup extends BaseGroup {

    /**
     * 卡片的背景
     */
    private Image bgImage;

    /**
     * 卡片上的数字标签
     */
    private Label dataLabel;
    /**
     * 卡片数字
     */
    private int data = 0;

    public CardGroup(MainGame mainGame, int data) {
        super(mainGame);
        init(data);
    }

    public void init(int data) {
        setSize((getMainGame().getWorldWidth() - 60) / 4 - 6, (getMainGame().getWorldWidth() - 60) / 4 - 6);

        bgImage = new Image(getMainGame().getTextureAtlas().findRegion(Res.Atlas.GAME_ROUND_RECT));
        bgImage.setScale(getWidth() / bgImage.getWidth(), getHeight() / bgImage.getHeight());
        addActor(bgImage);

        Label.LabelStyle style = new Label.LabelStyle();
        style.font = getMainGame().getBitmapFont();
        dataLabel = new Label("" + data, style);
        dataLabel.setFontScale(0.4f);
        //标签余文字同大小
        dataLabel.setSize(dataLabel.getPrefWidth(), dataLabel.getPrefHeight());
        dataLabel.setPosition(getWidth() / 2 - dataLabel.getWidth() / 2, getHeight() / 2 - dataLabel.getHeight() / 2);
        addActor(dataLabel);

        setData(data);
    }

    public void setData(int data) {
        this.data = data;
        setLabel(data);
        setColor(data);
    }

    /**
     * 设置卡片颜色
     *
     * @param data
     */
    private void setColor(int data) {
        switch (data) {
            case 2:
                bgImage.setColor(Res.CardColors.RGBA_2);
                break;
            case 4:
                bgImage.setColor(Res.CardColors.RGBA_4);
                break;
            case 8:
                bgImage.setColor(Res.CardColors.RGBA_8);
                break;
            case 16:
                bgImage.setColor(Res.CardColors.RGBA_16);
                break;
            case 32:
                bgImage.setColor(Res.CardColors.RGBA_32);
                break;
            case 64:
                bgImage.setColor(Res.CardColors.RGBA_64);
                break;
            case 128:
                bgImage.setColor(Res.CardColors.RGBA_128);
                break;
            case 256:
                bgImage.setColor(Res.CardColors.RGBA_256);
                break;
            case 512:
                bgImage.setColor(Res.CardColors.RGBA_512);
                break;
            case 1024:
                bgImage.setColor(Res.CardColors.RGBA_1024);
                break;
            case 2048:
                bgImage.setColor(Res.CardColors.RGBA_2048);
                break;
            case 4096:
                bgImage.setColor(new Color(799999999));
            default:
                bgImage.setColor(Res.CardColors.RGBA_0);
                break;
        }
    }

    /**
     * 重新设置文字，并且居中显示
     * data为0则不显示文字
     *
     * @param data
     */
    private void setLabel(int data) {
        if (data == 0) {
            dataLabel.setText("");
            return;
        }
        dataLabel.setText(data);
        dataLabel.setSize(dataLabel.getPrefWidth(),dataLabel.getPrefHeight());
        dataLabel.setPosition(getWidth() / 2 - dataLabel.getWidth() / 2, getHeight() / 2 - dataLabel.getHeight() / 2);
    }

    public int getData() {
        return data;
    }
}
